package com.javafactroy.kunlong.codefactroy.service.domain.valueobj;

import cn.hutool.core.util.StrUtil;
//import com.alibaba.excel.annotation.ExcelProperty;
import com.javafactroy.kunlong.base.metadata.valueobj.MetadataField;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class EntityProp extends MetadataField {

    String buildComment() {
        return StrUtil.format("\t\t// {} dataType:{}", StrUtil.isBlank(comment) ? colName : comment, dataType);

    }
    //@ExcelProperty("名称")

    String buildColAnnotation() {
        return StrUtil.format("\t\t@Column(name=\"{}\")", colName);

    }

    String buildEasyExcel() {
        return StrUtil.format("\t\t@ExcelProperty(\"{}\")", StrUtil.isBlank(comment) ? colName : comment);

    }

    String buildProp() {
        return StrUtil.format("\t\tprotected {}  {};", propType, propName);

    }

    boolean isPkey() {
        return colKey != null && colKey.equals("PRI");
    }

    String buildGeneratedValue() {
        if (isPkey()) {
            return StrUtil.format("\t\t@GeneratedValue(strategy = GenerationType.IDENTITY,generator = \"Mysql\")");
        }
        return "";
    }

    // @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "Mysql")
    String buildPkeyFlag() {
        if (isPkey()) {
            return StrUtil.format("\t\t@Id");
        }
        return "";
    }

    //	@ApiModelProperty(name="shop_name")
    String buildApiModelProp() {
        if (comment == null) {
            comment = colName;
        }
        return StrUtil.format("\t\t@ApiModelProperty(name=\"{}\")", getComment());

    }

    List<String> buildPropLine() {
        List<String> list = new ArrayList<>();
        list.add(buildComment());
        list.add(buildApiModelProp());
        list.add(buildEasyExcel());
        list.add(buildColAnnotation());

        if (isPkey()) {
            list.add(buildPkeyFlag());
            list.add(buildGeneratedValue());
        }
        list.add(buildProp());
        list.add("");
        return list;
    }

    ColPropValueObj makeColProp() {
        return new ColPropValueObj()
                .putParam("colName", colName).putParam("propName", propName).putParam("jdbcType", jdbcType);
    }

    ColEqValueObj makeCol() {
        if (dataType2PropType().equals("String")) {
            return new ColLikeValueObj().putParam("colName", colName).putParam("propName", propName).putParam("jdbcType", jdbcType);
        }
        return new ColEqValueObj().putParam("colName", colName).putParam("propName", propName).putParam("jdbcType", jdbcType);
    }

}
